package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Nivel;
import com.lostreseditores.sabersai.model.dto.NivelDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class NivelView {
    private InputText txtNivenombrev;
    private InputText txtNivepaginav;
    private InputText txtNiveidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<NivelDTO> data;
    private NivelDTO selectedNivel;

    public NivelView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            NivelDTO nivelDTO = (NivelDTO) e.getObject();

            if (txtNivenombrev == null) {
                txtNivenombrev = new InputText();
            }

            txtNivenombrev.setValue(nivelDTO.getNivenombrev());

            if (txtNivepaginav == null) {
                txtNivepaginav = new InputText();
            }

            txtNivepaginav.setValue(nivelDTO.getNivepaginav());

            if (txtNiveidn == null) {
                txtNiveidn = new InputText();
            }

            txtNiveidn.setValue(nivelDTO.getNiveidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtNivenombrev != null) {
            txtNivenombrev.setValue(null);
            txtNivenombrev.setDisabled(true);
        }

        if (txtNivepaginav != null) {
            txtNivepaginav.setValue(null);
            txtNivepaginav.setDisabled(true);
        }

        if (txtNiveidn != null) {
            txtNiveidn.setValue(null);
            txtNiveidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Nivel entity = null;

        try {
            Integer niveidn = new Integer(txtNiveidn.getValue().toString());
            entity = BusinessDelegatorView.getNivel(niveidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtNivenombrev.setDisabled(false);
            txtNivepaginav.setDisabled(false);
            txtNiveidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtNivenombrev.setValue(entity.getNivenombrev());
            txtNivenombrev.setDisabled(false);
            txtNivepaginav.setValue(entity.getNivepaginav());
            txtNivepaginav.setDisabled(false);
            txtNiveidn.setValue(entity.getNiveidn());
            txtNiveidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveNivel(FacesUtils.checkInteger(txtNiveidn),
                FacesUtils.checkString(txtNivenombrev),
                FacesUtils.checkString(txtNivepaginav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteNivel(FacesUtils.checkInteger(
                    txtNiveidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateNivel(FacesUtils.checkInteger(
                    txtNiveidn), FacesUtils.checkString(txtNivenombrev),
                FacesUtils.checkString(txtNivepaginav));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtNiveidn == null) {
                txtNiveidn = new InputText();
            }

            txtNiveidn.setValue(selectedNivel.getNiveidn());

            BusinessDelegatorView.deleteNivel(FacesUtils.checkInteger(
                    txtNiveidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedNivel);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer niveidn, String nivenombrev,
        String nivepaginav) throws Exception {
        try {
            BusinessDelegatorView.updateNivel(niveidn, nivenombrev, nivepaginav);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("NivelView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtNivenombrev() {
        return txtNivenombrev;
    }

    public void setTxtNivenombrev(InputText txtNivenombrev) {
        this.txtNivenombrev = txtNivenombrev;
    }

    public InputText getTxtNivepaginav() {
        return txtNivepaginav;
    }

    public void setTxtNivepaginav(InputText txtNivepaginav) {
        this.txtNivepaginav = txtNivepaginav;
    }

    public InputText getTxtNiveidn() {
        return txtNiveidn;
    }

    public void setTxtNiveidn(InputText txtNiveidn) {
        this.txtNiveidn = txtNiveidn;
    }

    public List<NivelDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataNivel();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<NivelDTO> nivelDTO) {
        this.data = nivelDTO;
    }

    public NivelDTO getSelectedNivel() {
        return selectedNivel;
    }

    public void setSelectedNivel(NivelDTO nivel) {
        this.selectedNivel = nivel;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
